import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.util.Iterator;
import java.util.List;


import org.jdom.Comment;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
//import org.jdom2.Document;
//import org.jdom2.Element;
//import org.jdom2.JDOMException;
//import org.jdom2.input.SAXBuilder;
import org.jdom.xpath.XPath;


public class XpathProcessing {
	public XpathProcessing(){
		
	}
	public static String  simplifyXpath(String strXpath,String content) throws  JDOMException, IOException 
	{
		strXpath=strXpath.replaceAll("^/html", "/");
		//System.out.println("<?xml version=\"1.0\"?>\n"+content.replaceAll("<!DOCTYPE[^>]*>", ""));;
		//ZFFile zff = new ZFFile(file, charset);
		//content = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"><html>\n<B></B><C><data ct=\"hhh\" >First</data><data ct=\"iii\" >Second</data></C>\n</html>";
		//int beginIndex = content.indexOf("\n");
		//content = content.substring(beginIndex);
		//System.out.println("<?xml version=\"1.0\"?>\n"+content.replaceAll("<!DOCTYPE[^>]*>", ""));
		//System.out.println("gagaga");
		//content="<a></a>";
		SAXBuilder builder = new SAXBuilder();
	    Document doc = builder.build(new StringReader("<?xml version=\"1.0\"?>\n"+content.replaceAll("<!doctype[^>]*>", "")));
	    //removeComments(doc);
	    //System.out.println(doc.);
		//Document doc = DocumentHelper.parseText(content.trim().replace("\n", ""));
		//@SuppressWarnings("deprecation")
	    //Element root = doc.getRootElement();
	    //System.out.println("bababa\n"+strXpath);
	    
		//@SuppressWarnings("deprecation")
		List result = XPath.selectNodes(doc,"//div[@id='endText']");
		//System.out.println("<?xml version=\"1.0\"?>\n"+content.replaceAll("<!doctype[^>]*>", ""));
		System.out.println("entered simplifyXpath"+"\t"+result.size());
	    
		if(result.size()!=1){System.out.println("Result not a unique !");return null;}
	    String spath="";
	    //System.out.println("lalala");
		while(true)
		{
			
			
			int index=strXpath.lastIndexOf("/");
			
			spath = "/"+strXpath.substring(index)+spath;
			strXpath = strXpath.substring(0, index);
			//System.out.println(strXpath);
			
			List sresult = XPath.selectNodes(doc,spath);
			System.out.println(spath+"\n"+sresult.size());
			if(sresult.size()!=1){System.out.println("SResult not a unique !");return null;}
			if(sresult.get(0).equals(result.get(0)))break;
			if(strXpath=="")break;
			//System.out.println(spath);
		}
		System.out.println("out simplifyXpath");
		return spath;
	}
	public static void removeComments(List l) {
        for (Iterator i = l.iterator(); i.hasNext();) {
            Object node = i.next();
            if (node instanceof Comment) {
                i.remove();
            } else if (node instanceof Element) {
                removeComments(((Element)node).getContent());
            }
        }
    }

    public static void removeComments(Document doc) {
        removeComments(doc.getContent());
    }
}
